﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace AnjLab.SyncIT.Core.Data.GoogleCode
{
    public class GoogleTimeStringConverter
    {
        /// <summary>
        /// Google's time representation in a web pages is a user friendly formatted string which may be of short and long formats.
        /// The short formatted strings may look like: "2 days ago", "35 minutes ago", "Sep 17", etc.
        /// The long formatted strings are: "Mar 12 (2 days ago)", "Sep 17, 2006", etc. 
        /// 
        /// This method converts short time string to System.DateTime instance relative to <code>now</code>.
        /// </summary>
        /// <param name="shortTime"></param>
        /// <param name="now"></param>
        /// <returns></returns>
        public static DateTime GoogleShortTimeStringToDateTime(string shortTime, DateTime now)
        {
            DateTime date;

            if (Regex.IsMatch(shortTime, "[(Jan)(Feb)(Mar)(Apr)(May)(Jun)(Jul)(Aug)(Sep)(Oct)(Nov)(Dec)] \\d+"))
            {
                string[] monthAndDay = shortTime.Split(' ');

                IDictionary<string, int> months = new Dictionary<string, int>(12)
                                                      {
                                                          {"Jan", 1},
                                                          {"Feb", 2},
                                                          {"Mar", 3},
                                                          {"Apr", 4},
                                                          {"May", 5},
                                                          {"Jun", 6},
                                                          {"Jul", 7},
                                                          {"Aug", 8},
                                                          {"Sep", 9},
                                                          {"Oct", 10},
                                                          {"Nov", 11},
                                                          {"Dec", 12}
                                                      };

                date = new DateTime(now.Year, months[monthAndDay[0]], int.Parse(monthAndDay[1]), now.Hour, now.Minute, now.Second);

                if (date.CompareTo(now) > 0)
                {
                    date = date.AddYears(-1);
                }

                return date;
            }

            if (shortTime.Equals("today"))
            {
                return now;
            }

            if (shortTime.Equals("moments ago"))
            {
                return now;
            }

            if (shortTime.EndsWith("ago"))
            {
                string[] offsetKind = shortTime.Split(' ');

                switch(offsetKind[1])
                {
                    case "days":
                    case "day":
                        return now.AddDays(-int.Parse(offsetKind[0]));
                        
                    case "weeks":
                    case "week":
                        return now.AddDays(-int.Parse(offsetKind[0]) * 7);

                    case "months":
                    case "month":
                        return now.AddMonths(-int.Parse(offsetKind[0]));

                    case "minutes":
                    case "minute":
                        return now.AddMinutes(-int.Parse(offsetKind[0]));

                    case "hours":
                    case "hour":
                        return now.AddHours(-int.Parse(offsetKind[0]));
                }
            }

            if (shortTime == "----")
            {
                return DateTime.MinValue;
            }

            //  TODO Investigate other shortTime formats
            throw new InvalidOperationException(string.Format("Unsupported shortDate format ({0})", shortTime));
        }
    }
}